﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using AppEntity;

namespace AppEntity
{
    public partial class App : IDisposable
    {
        private Common.DataBase.Sqlite Sqlite;

        public string ID;
        public XmlDocument AppDom;
        public string Name
        {
            get { return this.AppDom.SelectSingleNode("/root/title").InnerText; }
        }
        public DateTime DateCreated
        {
            get { return this.AppDom.SelectSingleNode("/root/datecreated").InnerText.ToDateTime(); }
        }
        public string Description
        {
            get { return this.AppDom.SelectSingleNode("/root/description").InnerText; }
        }    

        public App(string AppID)
        {
            this.Sqlite = new Common.DataBase.Sqlite(Base.GetDataBaseFilePath(AppID));
            this.AppDom = Base.GetAppSetting(AppID);            
            this.ID = AppID;            
        }

        public Entity GetEntity(string EntityID)
        {
            Entity En = new Entity(EntityID);
            En.AppID = this.ID;
            En.AppDom = this.AppDom;
            En.Name = this.AppDom.SelectSingleNode("/root/entities/entity[@name='" + EntityID + "']/@title").Value;
            En.App = this;
            return En;
        }

        public void Close()
        {
            this.Sqlite.Dispose();
        }

        /// <summary>
        /// 数据库最后的修改时间
        /// </summary>
        public DateTime DbLastWriteTime
        {
            get
            {
                return File.GetLastWriteTime(Base.GetDataBaseFilePath(this.ID));
            }
        }

        public void Dispose()
        {
            this.Close();
        }

    }
}
